from pygame import sprite
from pygame import image

BLKIMG = "C://brick.png"
CHARIMG = "C://char.png"

class AbstractGameObject(sprite.Sprite):
    
    def __init__(self, img):
        sprite.Sprite.__init__(self)
        self.image = image.load(img)
        self.rect = self.image.get_rect()
        self.newpos = self.rect
        self.pos = self.rect
        self.layer = 2
    
    
        
class Actor(AbstractGameObject):
    
    def __init__(self, img):
        
        super(Actor, self).__init__(img)
        self.layer = 1
        
    def update(self):
        
        self.rect = self.newpos
    
    def move(self, pos):

        self.newpos = self.rect.move(pos)
        #self.populate(rect)

class Grid(object):
    
    def __init__(self, width, height):
        
        if width and height:
            self.width = width
            self.height = height
            self.cells = [AbstractGameObject(BLKIMG) for cell in xrange(width * height)]
            self.grid = self.construct_grid()
            self.populate()
        else:
            raise StandardError("No size")
    
    def construct_grid(self):
        
        grid = []
        cells = self.cells
        columns = self.width
        while cells:
            if len(cells) > columns:
                row = cells[:columns]
                cells = cells[columns:]
            elif len(cells) == columns:
                row = cells
                cells = None
            else:
                row = cells
                for missing in xrange(columns - len(cells)):
                    row.append("Empty")
                cells = None
            grid.append(row)
        return grid
    
    def populate(self):
        
        for col_index, column in enumerate(self.grid):
            for row_index, cell in enumerate(column):
                block = AbstractGameObject(BLKIMG)
                y = 32 * col_index
                x = 32 * row_index
                block.rect = block.rect.move((x, y))
                self.grid[col_index][row_index] = block
                
                
    
    def __repr__(self):
        
        text = ''
        for x in self.grid:
            text = text + str(x) + "\n"
        return text


if __name__ == "__main__":
    #grid = Grid(10,10)
    #print grid
    a = Actor(BLKIMG)
    print a.rect
    a.move((0, 200))
    print a.rect